package cn.cangfei.massage.controller;

import cn.cangfei.massage.common.api.CommonPage;
import cn.cangfei.massage.common.api.CommonResult;
import cn.cangfei.massage.dto.param.MassageInfoParam;
import cn.cangfei.massage.dto.param.MassageOutpatientRelationParam;
import cn.cangfei.massage.dto.param.MassageSpecialRelationParam;
import cn.cangfei.massage.dto.update.MassageInfoUpdate;
import cn.cangfei.massage.entity.MassageInfo;
import cn.cangfei.massage.entity.MassageSpecial;
import cn.cangfei.massage.service.IMassageSpecialService;
import cn.cangfei.massage.service.IMassageInfoService;
import cn.cangfei.massage.service.IMassageOutpatientService;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author Lemon  2313812755@qq.com
 * @date 2023/2/3
 */

@Api(value = "推拿馆模块", tags = "推拿馆信息接口")
@RestController
@CrossOrigin
@RequestMapping("/massage")
public class MassageInfoController {

    @Resource
    private IMassageInfoService infoService;

    @Resource
    private IMassageSpecialService specialService;

    @Resource
    private IMassageOutpatientService outpatientService;

    @ApiOperation(value = "添加推拿馆信息", notes = "传入 推拿馆信息参数（名称，图片、电话，地址，简介）")
    @RequestMapping(value = "/info", method = RequestMethod.POST)
    public CommonResult insertMassageInfo(@RequestBody MassageInfoParam param) {


        if (infoService.insert(param)) {
            return CommonResult.success();
        }

        return CommonResult.failed("服务器错误，请联系管理员！");
    }

    @ApiOperation(value = "更新推拿馆信息", notes = "传入 推拿馆编号、推拿馆信息参数（名称，图片、电话，地址，简介）")
    @ApiImplicitParam(name = "id", value = "推拿馆编号", paramType = "path", dataType = "Long", required = true)
    @RequestMapping(value = "/info/{id}", method = RequestMethod.PUT)
    public CommonResult updateMassageInfo(@PathVariable Long id, @RequestBody MassageInfoParam param) {

        if (!infoService.count(id)) {
            return CommonResult.validateFailed("不存在，该推拿馆编号！");
        }

        if (infoService.update(id, param)) {
            return CommonResult.success();
        }

        return CommonResult.failed("服务器错误，请联系管理员！");
    }

    @ApiOperation(value = "删除推拿馆信息", notes = "传入 推拿馆编号")
    @ApiImplicitParam(name = "id", value = "推拿馆编号", paramType = "path", dataType = "Long", required = true)
    @RequestMapping(value = "/info/{id}", method = RequestMethod.DELETE)
    public CommonResult deleteMassageInfo(@PathVariable Long id) {

        if (!infoService.count(id)) {
            return CommonResult.validateFailed("不存在，该推拿馆编号！");
        }

        if (infoService.delete(id)) {
            return CommonResult.success();
        }

        return CommonResult.failed("服务器错误，请联系管理员！");
    }

    @ApiOperation(value = "分页：搜索推拿馆信息", notes = "传入 推拿馆名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "推拿馆名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "第几页", paramType = "query", dataType = "Integer",
                    required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", paramType = "query", dataType = "Integer",
                    required = true)
    })
    @RequestMapping(value = "/info/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<MassageInfo>> searchMassageInfo(@RequestParam(required = false) String name,
                                                                   @RequestParam Integer pageNum,
                                                                   @RequestParam Integer pageSize) {

        return CommonResult.success(CommonPage.restPage(infoService.list(name, pageNum, pageSize)));
    }

    @ApiOperation(value = "添加专科到推拿馆中", notes = "传入 推拿馆专科关系参数（推拿馆编号、专科编号）")
    @RequestMapping(value = "/special/relation", method = RequestMethod.POST)
    public CommonResult insertSpecialRelation(@RequestBody MassageSpecialRelationParam param) {

        if (!infoService.count(param.getMassageId())) {
            return CommonResult.validateFailed("不存在，该推拿馆编号！");
        }

        if (!specialService.count(param.getSpecialId())) {
            return CommonResult.validateFailed("不存在，该专科编号! ");
        }

        if (infoService.countSpecialRelation(param)) {
            return CommonResult.validateFailed("已存在，该专科关系！");
        }

        if (infoService.insertSpecialRelation(param)) {
            return CommonResult.success();
        }

        return CommonResult.failed("服务器错误，请联系管理员！");
    }

    @ApiOperation(value = "根据推拿馆id获取推拿馆信息", notes = "传入 参数（推拿馆编号）")
    @RequestMapping(value = "/special/infoById", method = RequestMethod.GET)
    public CommonResult getMassageById(@RequestParam Long massageId) {

        if (!infoService.count(massageId)) {
            return CommonResult.validateFailed("不存在，该推拿馆编号！");
        }

        MassageInfoUpdate massage = infoService.getMassageById(massageId);

        return CommonResult.success(massage);
    }

    @ApiOperation(value = "移除推拿馆中的专科", notes = "传入 推拿馆编号、专科编号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "massageId", value = "推拿馆编号", paramType = "query", dataType = "Long"
                    , required = true),
            @ApiImplicitParam(name = "specialId", value = "专科编号", paramType = "query", dataType = "Long"
                    , required = true)
    })
    @RequestMapping(value = "/special/relation", method = RequestMethod.DELETE)
    public CommonResult deleteSpecialRelation(@RequestParam Long massageId, @RequestParam Long specialId) {

        if (!infoService.count(massageId)) {
            return CommonResult.validateFailed("不存在，该推拿馆编号！");
        }

        if (!specialService.count(specialId)) {
            return CommonResult.validateFailed("不存在，该专科编号！");
        }

        if (infoService.deleteSpecialRelation(massageId, specialId)) {
            return CommonResult.success();
        }

        return CommonResult.failed("服务器错误，请联系管理员！");
    }

    @ApiOperation(value = "添加门诊到推拿馆中", notes = "传入 推拿馆门诊关系参数（推拿馆编号、门诊编号）")
    @RequestMapping(value = "/outpatient/relation", method = RequestMethod.POST)
    public CommonResult insertOutpatientRelation(@RequestBody MassageOutpatientRelationParam param) {

        if (!infoService.count(param.getMassageId())) {
            return CommonResult.validateFailed("不存在，该推拿馆编号！");
        }

        if (!outpatientService.count(param.getOutpatientId())) {
            return CommonResult.validateFailed("不存在，该门诊编号! ");
        }

        if (infoService.countOutpatientRelation(param)) {
            return CommonResult.validateFailed("已存在，该门诊关系！");
        }

        if (infoService.insertOutpatientRelation(param)) {
            return CommonResult.success();
        }

        return CommonResult.failed("服务器错误，请联系管理员！");

    }

    @ApiOperation(value = "移除推拿馆中的门诊", notes = "传入 推拿馆编号、门诊编号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "massageId", value = "推拿馆编号", paramType = "query", dataType = "Long"
                    , required = true),
            @ApiImplicitParam(name = "outpatientId", value = "门诊编号", paramType = "query", dataType = "Long"
                    , required = true)
    })
    @RequestMapping(value = "/outpatient/relation", method = RequestMethod.DELETE)
    public CommonResult deleteOutpatientRelation(@RequestParam Long massageId, @RequestParam Long outpatientId) {

        if (!infoService.count(massageId)) {
            return CommonResult.validateFailed("不存在，该推拿馆编号！");
        }

        if (!outpatientService.count(outpatientId)) {
            return CommonResult.validateFailed("不存在，该门诊编号! ");
        }

        if (infoService.deleteOutpatientRelation(massageId, outpatientId)) {
            return CommonResult.success();
        }

        return CommonResult.failed("服务器错误，请联系管理员！");
    }

    @ApiOperation(value = "获取推拿馆，所属专科列表", notes = "传入 专科名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "massageId", value = "推拿馆编号", paramType = "path", dataType = "Long",
                    required = true),
            @ApiImplicitParam(name = "pageNum", value = "第几页", paramType = "query", dataType = "Integer",
                    required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", paramType = "query", dataType = "Integer",
                    required = true)
    })
    @RequestMapping(value = "/special/list/{massageId}", method = RequestMethod.GET)
    public CommonResult<CommonPage<MassageSpecial>> listSpecialByMassage(@PathVariable Long massageId,
                                                                         @RequestParam Integer pageNum,
                                                                         @RequestParam Integer pageSize) {

        if (!infoService.count(massageId)) {
            return CommonResult.validateFailed("不存在，该推拿馆编号！");
        }

        return CommonResult.success(CommonPage.restPage(specialService.list(massageId, pageNum, pageSize)));
    }
}
